# -*- coding: utf-8 -*-

from PyQt4 import QtCore
from PyQt4 import QtGui


class CheckBox(QtGui.QCheckBox):
    def __init__(self, value=None):
        super(CheckBox, self).__init__()
        self.setValue(value)

    def getValue(self):
        return self.isChecked()

    def setValue(self, value):
        if value:
            self.setChecked = value

    def __repr__(self):
        return "CheckBox<%s>" % id(self)


class AnswerLine(QtGui.QLineEdit):
    def __init__(self, value=None):
        super(AnswerLine, self).__init__()
        self.setValue(value)

    def getValue(self):
        return self.text()

    def setValue(self, value):
        if value:
            self.setText(value)

    def __repr__(self):
        return "AnswerLine<%s>" % id(self)


class RadioButton(QtGui.QRadioButton):
    def __init__(self, value=None):
        super(RadioButton, self).__init__()
        self.setValue(value)

    def getValue(self):
        if self.isChecked():
            return "="
        else:
            return "~"

    def setValue(self, value):
        if value:
            self.setChecked(value)

    def __repr__(self):
        return "RadioButton<%s>" % id(self)


class MatchingLabel(QtGui.QLabel):
    def __init__(self):
        super(MatchingLabel, self).__init__()
        self.setText("->")

    def getValue(self):
        return self.text()

    def setValue(self, value):
        pass


class PercentWidget(QtGui.QWidget):
    def __init__(self, value = None):
        super(PercentWidget, self).__init__()

        # hodnoty do comboboxu
        percentValues = ("100", "90", "80", "75", "70", "66.666", "60", "50", "40", "33.333", "30", "25", "20", "16.666", "14.2857", "12.5", "11.111", "10", "5")
        negativePercentValues = ["-%s" % x for x in percentValues]
        validator = QtGui.QDoubleValidator(-100, 100, 1000, self)

        layout = QtGui.QHBoxLayout()
        layout.setContentsMargins(0, 0, 0, 0)
        # checkbox ktery aktivuje ComboBox s vyberem hodnot
        self.enablePercentWidget = QtGui.QCheckBox()
        layout.addWidget(self.enablePercentWidget)

        self.percentBox = QtGui.QComboBox()
        self.percentBox.setSizePolicy(QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Minimum)
        self.percentBox.setEditable(True)
        self.percentBox.setDisabled(True)
        self.percentBox.setValidator(validator)
        self.percentBox.addItems(percentValues)
        self.percentBox.addItems(negativePercentValues)
        layout.addWidget(self.percentBox)

        percentLabel = QtGui.QLabel("%")
        percentLabel.setSizePolicy(QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Minimum)
        layout.addWidget(percentLabel)

        self.setLayout(layout)

        QtCore.QObject.connect(self.enablePercentWidget, QtCore.SIGNAL("stateChanged(int)"), self.checkboxChange)


    def checkboxChange(self, int):
        """ vola se pri zmene checkboxu, zpristupni/vypne spinbox vedle """

        self.percentBox.setEnabled(int)
        # dame percent boxu focus, aneb  co me naucilo dgxovo video o pouzitelnosti MacOS :)
        self.percentBox.setFocus()
        # oznacime hodnotu ve spinboxu, aby bylo mozno ji hned z klavesnice prepsat
        self.percentBox.lineEdit().selectAll()

        #self.validatePercent()

    def getValue(self):
        if self.percentBox.isEnabled():
            return "%s%s%s" % ("%", self.percentBox.currentText(), "%")
        else:
            return QtCore.QString()

    def setValue(self, value):
        if value:
            # odemkneme combobox, checkbox
            self.enablePercentWidget.setChecked(True)
            self.percentBox.setEnabled(True)
            # kouknem jestli hodnota je jedna z prednastavenych
            valueIndex = self.percentBox.findText(value)
            if valueIndex != -1:
                self.percentBox.setCurrentIndex(valueIndex)
            else:
                # pokud ji tam nemame, tak ji vlozime
                self.percentBox.addItem(value)
                valueIndex = self.percentBox.findText(value)
                self.percentBox.setCurrentIndex(valueIndex)

    def __repr__(self):
        return "PercentWidget<%s>" % id(self)

